import util from "../../util";
import parentInstance from "../parentInstance";
export default class bi extends parentInstance{
    constructor() {
        super();
        this.util = util;
        this.typeList=['BI'];
        this.record = {};
        this.info = null;
        this.pointer = null;
        this.modelMap=null;
        /** modelMap格式
         * {
            table: tableName,
            featureList: new Set(),
            valueList: new Set(),
            fieldList: new Set(),
            groupList: new Set(),
            fieldMap: {},
          }
         */
    }
    static create(record){
        let object=new bi();
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    setRecord(record){
        if(!!record.type&&!!record.className){
            this.record=record;
            if(!record.web||Object.keys(record.web).length===0){
                this.record.web=bi.webTpl();
            }
            this._setPointer();
        }else{
            this.record={};
            this._setPointer(false);
        }

    }
    _setPointer(empty=true){
        if(!empty){
            this.info=null;
            this.pointer=null;
            this.modelMap=null;
            return;
        }
        this.pointer={
            type:this.record.type,
            className:this.record.className,
        };
        this.info={
            type:this.record.type,
            className:this.record.className,
            version:this.record.version,
            published:this.record.published,
        };
        this.modelMap=this._getModelMap();
    }
    _getFilter(array){
        let filter='';
        if(util.tools.testType(array)=='array'){
            filter= `type='${array[0]}'&&className='${array[1]}'`
        }else if(util.tools.testType(array)=='object'){
            filter= `type='${array.category}'&&className='${array.name}'`
        }else if(util.tools.testType(array)=='string'&&array.split(',').length==2){
            let arr=array.split(',');
            filter= `type='${arr.category}'&&className='${arr.name}'`
        }else{
            filter= array;
        }
        filter=`${filter}`
        return filter;
    }
    _getModelMap(){
        return util.modelState.map.get([this.pointer.type,this.pointer.className].join(','));
    }
    async add(record){
        if(!record||!record.type||!record.className||record.type==''||record.className==''){
            return false;
        }
        let newRecord=Object.assign(bi.tpl(),record);
        let result = await util.api.BiInstance.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async delete(){
        if(!!this.pointer){
            let result = await util.api.BiInstance.delete(this.record.id).then(flag=>{
                if(flag){
                    this.setRecord([]);
                    return true;
                }else{
                    return false;
                }
            }).catch(err=>{
                return false;
            });
            return result;
        }else{
            return false;
        }

    }
    async update(){
        let record=util.lodash.cloneDeep(this.record);
        this.record=record;
        let result = await util.api.BiInstance.update(this.record).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async query(param){
        let filter=this._getFilter(param)
        let result = await util.api.BiInstance.query({condition:filter}).then(data=>{
            if(!!data){
                this.setRecord(data[0]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    static tpl(){
        return {
            type:"",
            className:"",
            version: 1,
            published: true,
            values:{},
            hier:{},
            web:{},
            others:{},
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: ""
        }
    }
    static webTpl(){
        return {
            D2:bi.D2Tpl(),
            D3:bi.D3Tpl(),
        }
    }
    /**D2可视化*/
    addD2Element(name){
        this.record.web.D2.element.push({...bi.D2ElementItemTpl(),...{name}})
    }
    removeD2Element(index){
        this.record.web.D2.element.splice(index,1);
    }
    addD2Baseline(){
        this.record.web.D2.baseline.push(bi.D2BaselineTpl())
    }
    removeD2Baseline(index){
        this.record.web.D2.baseline.splice(index,1);
    }
    addD2Lamp(){
        this.record.web.D2.lamp.push(bi.D2LampItemTpl())
    }
    removeD2Lamp(index){
        this.record.web.D2.lamp.splice(index,1);
    }
    addD2Connect(){
        this.record.web.D2.connect.push(bi.D2ConnectItemTpl())
    }
    removeD2Connect(index){
        this.record.web.D2.connect.splice(index,1);
    }
    addD2Text(){
        this.record.web.D2.text.push(bi.D2TextItemTpl())
    }
    removeD2Text(index){
        this.record.web.D2.text.splice(index,1);
    }
    addD2SignalText(){
        this.record.web.D2.signalText.push(bi.D2SignalTextItemTpl())
    }
    removeD2SignalText(index){
        this.record.web.D2.signalText.splice(index,1);
    }
    static D2Tpl(){
        return {
            "width": "100%",
            "height": 800,
            "background": "black",
            "timer": "timer",
            "dur": 10,
            "baseline": [],
            "element": [],
            "lamp": [],
            "connect": [],
            "text": [],
            "signalText": []
        }
    }
    static D2ElementItemTpl(){
        return {
            "x": 269,
            "y": 96,
            "scaleX": 0.4,
            "scaleY": 0.4,
            "name": ""
        }
    }
    static D2BaselineTpl(){
        return {
            "enable": true,
            "x1": 0,
            "y1": 200,
            "x2": 1200,
            "y2": 200
        }
    }
    static D2LampItemTpl(){
        return {
            "x": 353,
            "y": 145,
            "mark": [
                "red",
                "yellow",
                "green"
            ],
            "signal": "mark1"
        }
    }
    static D2ConnectItemTpl(){
        return {
            "x1": 372,
            "y1": 190,
            "x2": 542,
            "y2": 190,
            "rotate": 0,
            "width": 15,
            "height": 10,
            "interval": 5,
            "color": "gray",
            "fill": "red",
            "dur": 10
        }
    }
    static D2TextItemTpl(){
        return {
            "inner": "高炉:",
            "x": 274,
            "y": 72,
            "fill": "white",
            "size": "14px"
        }
    }
    static D2SignalTextItemTpl(){
        return {
            "mark": "textMark1",
            "x": 310,
            "y": 72,
            "fill": "red",
            "size": "14px"
        }
    }

    /**D3可视化*/
    addD3Element(name){
        this.record.web.D3.element.push({...bi.D3ElementItemTpl(),...{name}})
    }
    removeD3Element(index){
        this.record.web.D3.element.splice(index,1);
    }
    addD3Connect(){
        this.record.web.D3.connect.push(bi.D3ConnectItemTpl())
    }
    removeD3Connect(index){
        this.record.web.D3.connect.splice(index,1);
    }
    addD3MoveItem(){
        this.record.web.D3.moveItem.push(bi.D3MoveItemTpl())
    }
    removeD3MoveItem(index){
        this.record.web.D3.moveItem.splice(index,1);
    }
    addD3Anim(){
        this.record.web.D3.anim.push(bi.D3AnimItemTpl())
    }
    removeD3Anim(index){
        this.record.web.D3.anim.splice(index,1);
    }
    addD3AnimAction(animIndex,name){
        this.record.web.D3.anim[animIndex].action.push({...bi.D3AnimActionItemTpl(name),...{name}})
    }
    removeD3AnimAction(animIndex,index){
        this.record.web.D3.anim[animIndex].action.splice(index,1);
    }
    static D3Tpl(){
        return {
            "environment": {
                "s": 110,
                "render": {
                    "clearColor": "black",
                    "opacity": 1
                },
                "helper": {
                    "plane": false,
                    "camera": false,
                    "axes": true
                },
                "point": [
                    {
                        "x": -130,
                        "y": 105,
                        "z": 100,
                        "c": "#EAEAEF"
                    }
                ],
                "ambient": {
                    "c": "#888888"
                },
                "camera": {
                    "type": "OrthographicCamera",
                    "x": -400,
                    "y": 200,
                    "z": -200,
                    "n": 1,
                    "f": 1000
                }
            },
            "plant": {
                "width": 240,
                "height": 2,
                "depth": 350,
                "color": "#b7c8b5",
                "x": 0,
                "y": 0,
                "z": 0,
                "rx": 0,
                "ry": 0,
                "rz": 0
            },
            "element": [],
            "connect":[],
            "moveItem":[],
            "anim":[]
        }
    }
    static D3ElementItemTpl() {
        return {
            "name": "高炉",
            "position": {
                "x": 80,
                "y": 10,
                "z": -120,
                "rx": 0,
                "ry": 0,
                "rz": 0,
                "scaleX": 1,
                "scaleY": 1,
                "scaleZ": 1
            }
        }
    }
    static D3ConnectItemTpl(){
        return {
            "name": "roll1",
            "length": 50,
            "color": "#708090",
            "interval": 6,
            "intervalColor": "#BEBEBE",
            "x": 75,
            "y": 2,
            "z": -95,
            "rx": 0,
            "ry": 0,
            "rz": 0,
            "scaleX": 1,
            "scaleY": 1,
            "scaleZ": 1
        }
    }
    static D3MoveItemTpl(){
        return {
            "name": "moveItem",
            "width": 10,
            "height": 2,
            "depth": 10,
            "color": "red",
            "x": 80,
            "y": 3,
            "z": -105
        }
    }
    static D3AnimItemTpl(){
        return {
            "name": "roll1Move",
            "duration": 10,
            "action":[]
        }
    }
    static D3AnimActionItemTpl(name){
        if(name==='position'){
            return {
                "name": "position",
                "times": [
                    1,
                    10
                ],
                "values": [
                    80,
                    3,
                    -105,
                    80,
                    3,
                    -75
                ]
            }
        }else if(name==='color'){
            return {
                "name": "color",
                "times": [
                    1,
                    10
                ],
                "values": [
                    "#FF0000",
                    "#FFFFFF"
                ]
            }
        }
    }

    /*static D3ElementItemTpl(){
        return {
            "environment": {
                "s": 110,
                "render": {
                    "clearColor": "black",
                    "opacity": 1
                },
                "helper": {
                    "plane": false,
                    "camera": false,
                    "axes": true
                },
                "point": [
                    {
                        "x": -130,
                        "y": 105,
                        "z": 100,
                        "c": "#EAEAEF"
                    }
                ],
                "ambient": {
                    "c": "#888888"
                },
                "camera": {
                    "type": "OrthographicCamera",
                    "x": -400,
                    "y": 200,
                    "z": -200,
                    "n": 1,
                    "f": 1000
                }
            },
            "plant": {
                "width": 240,
                "height": 2,
                "depth": 350,
                "color": "#b7c8b5",
                "x": 0,
                "y": 0,
                "z": 0,
                "rx": 0,
                "ry": 0,
                "rz": 0
            },
            "element": [
                {
                    "name": "高炉",
                    "position": {
                        "x": 80,
                        "y": 10,
                        "z": -120,
                        "rx": 0,
                        "ry": 0,
                        "rz": 0,
                        "scaleX": 1,
                        "scaleY": 1,
                        "scaleZ": 1
                    }
                }
            ],
            "connect": [
                {
                    "name": "roll1",
                    "length": 50,
                    "color": "#708090",
                    "interval": 6,
                    "intervalColor": "#BEBEBE",
                    "x": 75,
                    "y": 2,
                    "z": -95,
                    "rx": 0,
                    "ry": 0,
                    "rz": 0,
                    "scaleX": 1,
                    "scaleY": 1,
                    "scaleZ": 1
                }
            ],
            "moveItem": [
                {
                    "name": "roll1Move",
                    "width": 10,
                    "height": 2,
                    "depth": 10,
                    "color": "red",
                    "x": 80,
                    "y": 3,
                    "z": -105
                }
            ],
            "anim": [
                {
                    "name": "roll1Move",
                    "duration": 10,
                    "action": [
                        {
                            "name": "position",
                            "times": [
                                1,
                                10
                            ],
                            "values": [
                                80,
                                3,
                                -105,
                                80,
                                3,
                                -75
                            ]
                        },
                        {
                            "name": "color",
                            "times": [
                                1,
                                10
                            ],
                            "values": [
                                "#FF0000",
                                "#FFFFFF"
                            ]
                        }
                    ]
                }
            ]
        }
    }*/

}
